package com.dawan.sbjy.Config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

@Aspect
    @Component
public class AopLogWebapi3 {
    private static Logger logger = LoggerFactory.getLogger(AopLogWebapi3.class);
        @Pointcut("execution(public * com.dawan.sbjy.controller.*.*(..))orexecution(public * com.dawan.sbjy.service.*.*(..))")

        public void log(){}

        /**统一设置请求日志记录;

         * @Before 在方法执行之前执行

         * */
        @Before("log()")

        public void doBefore(JoinPoint joinPoint){

        // logger.info("doBefore");

        //记录http请求

            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

            HttpServletRequest request = attributes.getRequest();

        //从request中获取http请求的url/请求的方法类型／响应该http请求的类方法／IP地址／请求中的参数
        //url
            logger.info("url={}",request.getRequestURI());
        //method
            logger.info("method={}",request.getMethod());

        //ip

            logger.info("ip={}",request.getRemoteAddr());

        //类方法

            logger.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+

                    "."+joinPoint.getSignature().getName());


//参数


// logger.info("args={}",joinPoint.getArgs());


            logger.info("ARGS={}", Arrays.toString(joinPoint.getArgs()));



        }

        /**

         * 因返回类型不统一;所以使用object接收;

         */

        @AfterReturning(returning = "object",pointcut = "log()")

        public void doAfterReturning(Object object){

            logger.info(object.toString()+"123");

        }

}